package com.wyu.dao;

import com.alibaba.druid.util.JdbcUtils;
import com.wyu.pojo.YG;
import com.wyu.util.GetCt;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class SelectDaoImpl implements SelectDao {
    @Override
    public List<YG> searchygh(String ygh) {
        QueryRunner qr = new QueryRunner();
        Connection ct = null;
        YG yg = null;
        //获得数据库连接，德鲁伊数据源
        try {
            ct = GetCt.getConnection();
            String sql = "select * from ygb where bz!=1 and ygh like ?;";
            List<String> list3 = new ArrayList<>();
            if(ygh!=null){
                list3.add("%"+ygh+"%");
            }if (ygh==null){
                return null;
            }
            BeanListHandler<YG> handler = new BeanListHandler(YG.class);
            List<YG> list2 = qr.query(ct,sql,handler,list3.toArray());
            System.out.println(list2);
            return list2;
        } catch (Exception exception){
            exception.printStackTrace();
        }finally {
            JdbcUtils.close(ct);
        }
        return null;
    }

    @Override
    public List<YG> searchxm(String xm) {
        QueryRunner qr = new QueryRunner();
        Connection ct = null;
        YG yg = null;
        //获得数据库连接，德鲁伊数据源
        try {
            ct = GetCt.getConnection();
            String sql = "select * from ygb where bz!=1 and  xm like ? ";
            List<String> list4 = new ArrayList<>();
            if(xm!=null){
                list4.add("%"+xm+"%");
            }if (xm==null){
                return null;
            }
            BeanListHandler<YG> handler = new BeanListHandler(YG.class);
            List<YG> list1 = qr.query(ct,sql,handler,list4.toArray());
            System.out.println(list1);
            return list1;
        } catch (Exception exception){
            exception.printStackTrace();
        }finally {
            JdbcUtils.close(ct);
        }
        return null;
    }
}
